//
//  LPFStatusToolBarView.swift
//  WeiBo
//
//  Created by mac on 16/4/20.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
//转发 评论 赞 点击事件
enum LPFStatusToolBarViewType: Int{
    case retweet = 3001
    case comment = 3002
    case unlike = 3003
}

class LPFStatusToolBarView: UIView {
    
    //转发 评论 赞 点击事件
    var statusToolBarViewClosure: ((type: LPFStatusToolBarViewType) -> ())?
    
    // 转发
    var retweetButton: UIButton?
    
    // 评论
    var commentButton: UIButton?
    
    // 赞
    var unlikeButton: UIButton?
    
    var status: LPFStatusModel?{
        didSet{
            let retweetResult = getCountString(status?.reposts_count ?? 0, tilte: "转发")
            let commentResult = getCountString(status?.comments_count ?? 0, tilte: "评论")
            let unlikeResult = getCountString(status?.attitudes_count ?? 0, tilte: "赞")
            
            retweetButton?.setTitle(retweetResult, forState: UIControlState.Normal)
            
            commentButton?.setTitle(commentResult, forState: UIControlState.Normal)
            
            unlikeButton?.setTitle(unlikeResult, forState: UIControlState.Normal)
        }
    }
    
    //计算转发 评论 赞 的个数
    func getCountString(count: Int, tilte: String) -> String{
        if count <= 0{
            return tilte
        }else if count > 0 && count < 10000{
            return "\(count)"
        }else{
            // 转成 CGFloat
            let num = CGFloat(count / 1000) / 10
            // Int 转字符串
            var countString = "\(num)万"
            
            // 判断我们的字符串中 是否有'.0'
            
            if countString.containsString(".0"){
                countString = countString.stringByReplacingOccurrencesOfString(".0万", withString: "万")
            }
            
            return countString
            
        }
    }
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        self.backgroundColor = RGB(red: 237, green: 237, blue: 237)
        
        //添加控件
        retweetButton = addChildButton("timeline_icon_retweet", title: "转发", type:.retweet)
        commentButton =  addChildButton("timeline_icon_comment", title: "评论", type: .comment)
        unlikeButton =  addChildButton("timeline_icon_unlike", title: "赞", type: .unlike)
        
        let line1 = addChildLineImageView()
        let line2 = addChildLineImageView()
        
        // 添加约束
        // 01
        retweetButton!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.leading.equalTo(self)
            make.width.equalTo(commentButton!)
        }
        
        // 02
        commentButton!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(retweetButton!.snp_trailing)
            make.width.equalTo(unlikeButton!)
        }
        
        // 03
        unlikeButton!.snp_makeConstraints { (make) -> Void in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(commentButton!.snp_trailing)
        }
        
        // 04
        line1.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(retweetButton!.snp_trailing)
            make.centerY.equalTo(retweetButton!)
        }
        
        // 05
        line2.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(commentButton!.snp_trailing)
            make.centerY.equalTo(commentButton!)
        }
    }
    
    // MARK: - 创建button
    func addChildButton(imgName: String, title: String, type: LPFStatusToolBarViewType) -> UIButton{
        let btn = UIButton()
        
        //添加tag值
        btn.tag = type.rawValue
        //添加点击事件
        btn.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
        
        btn.setTitle(title, forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        btn.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        addSubview(btn)
        
        return btn
    }
    
    // 创建竖线
    func addChildLineImageView() -> UIImageView{
        let img = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        addSubview(img)
        return img
    }
    
    // MARK: - 转发 评论 赞 点击事件
    @objc private func buttonClick(button: UIButton){
        statusToolBarViewClosure?(type: LPFStatusToolBarViewType(rawValue: button.tag)!)
    }

}
